package redisclient

import (
	"gitee.com/lsy007/redisclient/model"
	"github.com/pquerna/ffjson/ffjson"
)

// SAdd(key string, values ...string) (int64,err error)
// SCard(key string) (int64,err error)
// SDiff(keys ...string) ([]string,err error)
// SDiffStore(destination string, keys ...string) (int64,err error)
// SInter(keys ...string) ([]string,err error)
// SInterStore(destination string, keys ...string) (int64,err error)
// SIsMember(key string, value string) (bool,err error)
// SMembers(key string) ([]string,err error)
// SMove(source string, destination string, value string) (bool,err error)
// SPop(key string) (string,err error)
// SRandMember(key string) (string,err error)
// SRem(key string, values ...string) (int64,err error)
// SUnion(keys ...string) ([]string,err error)
// SUnionStore(destination string, keys ...string) (int64,err error)

// 向集合添加一个或多个成员
func (c *RedisClient) SAdd(key string, members ...string) (count int64, err error) {
	var bytes []byte
	if bytes, err = ffjson.Marshal(members); err != nil {
		return
	}
	args := &model.RedisArgsModel{Key: key, Bytes: bytes}
	response, err = c.Request("SAdd", "Set", args)
	return response.Int, err
}

// 获取集合的成员数
func (c *RedisClient) SCard(key string) (count int64, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("SCard", "Set", args)
	return response.Int, err
}

// 返回第一个集合与其他集合之间的差异，切片第一个值为第一个集合键值
func (c *RedisClient) SDiff(keys ...string) (members []string, err error) {
	args := &model.RedisArgsModel{Keys: keys}
	response, err = c.Request("SDiff", "Set", args)
	return response.StringSlice, err
}

// 返回给定所有集合的差集并存储在 destination 中
func (c *RedisClient) SDiffStore(destination string, keys ...string) (count int64, err error) {
	args := &model.RedisArgsModel{Destination: destination, Keys: keys}
	response, err = c.Request("SDiffStore", "Set", args)
	return response.Int, err
}

// 返回给定所有集合的交集
func (c *RedisClient) SInter(keys ...string) (members []string, err error) {
	args := &model.RedisArgsModel{Keys: keys}
	response, err = c.Request("SInter", "Set", args)
	return response.StringSlice, err
}

// 返回给定所有集合的交集并存储在 destination 中
func (c *RedisClient) SInterStore(destination string, keys ...string) (count int64, err error) {
	args := &model.RedisArgsModel{Destination: destination, Keys: keys}
	response, err = c.Request("SInterStore", "Set", args)
	return response.Int, err
}

// 判断 member 元素是否是集合 key 的成员
func (c *RedisClient) SIsMember(key string, member string) (status bool, err error) {
	args := &model.RedisArgsModel{Key: key, Value: member}
	response, err = c.Request("SIsMember", "Set", args)
	return response.Bool, err
}

// 返回集合中的所有成员
func (c *RedisClient) SMembers(key string) (members []string, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("SMembers", "Set", args)
	return response.StringSlice, err
}

// 将 member 元素从 source 集合移动到 destination 集合
func (c *RedisClient) SMove(source string, destination string, member string) (status bool, err error) {
	args := &model.RedisArgsModel{Source: source, Destination: destination, Value: member}
	response, err = c.Request("SMove", "Set", args)
	return response.Bool, err
}

// 随机移除并返回集合中的一个元素
func (c *RedisClient) SPop(key string) (member string, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("SPop", "Set", args)
	return response.String, err
}

// 随机返回集合中一个成员
func (c *RedisClient) SRandMember(key string) (member string, err error) {
	args := &model.RedisArgsModel{Key: key}
	response, err = c.Request("SRandMember", "Set", args)
	return response.String, err
}

// 移除集合中一个或多个成员
func (c *RedisClient) SRem(key string, members ...string) (count int64, err error) {
	var bytes []byte
	if bytes, err = ffjson.Marshal(members); err != nil {
		return
	}
	args := &model.RedisArgsModel{Key: key, Bytes: bytes}
	response, err = c.Request("SRem", "Set", args)
	return response.Int, err
}

// 返回所有给定集合的并集
func (c *RedisClient) SUnion(keys ...string) (members []string, err error) {
	args := &model.RedisArgsModel{Keys: keys}
	response, err = c.Request("SUnion", "Set", args)
	return response.StringSlice, err
}

// 所有给定集合的并集存储在 destination 集合中
func (c *RedisClient) SUnionStore(destination string, keys ...string) (count int64, err error) {
	args := &model.RedisArgsModel{Destination: destination, Keys: keys}
	response, err = c.Request("SUnionStore", "Set", args)
	return response.Int, err
}

// 迭代获取集合中的元素
func (c *RedisClient) SScan(key string, cursor int64, match string, count int64) (members []string, nextCursor int64, err error) {
	args := &model.RedisArgsModel{Key: key, Cursor: cursor, Match: match, Count: count}
	response, err = c.Request("SScan", "Set", args)
	return response.StringSlice, response.Int, err
}
